class ReasoningServer extends Ice.Application
{
    
    public int run(String[] args)
    {
	Ice.ObjectAdapter adapter;
	Ice.ObjectPrx subscriber;
	Boolean persistence = true;
	Boolean anormality = true;
	Boolean alarms = true;

	Ice.Properties properties = communicator().getProperties();
	String topic_name = properties.getProperty("video_topic");
	String aux1 = properties.getProperty("persistence");
	String aux2 = properties.getProperty("anormality");
	String aux3 = properties.getProperty("alarms");
	String aux4 = properties.getProperty("camera");
	System.out.println(aux4);

	if (aux1 == "no")
	    persistence = false;

	if (aux2 == "no")
	    anormality = false;

	if (aux3 == "no")
	    alarms = false;

	adapter = communicator().createObjectAdapter("ReasoningServerAdapter");
	subscriber = adapter.add(new ReasoningCoreI(adapter.getCommunicator(), persistence, anormality, alarms),
			  communicator().stringToIdentity("ReasoningCore" + "." + aux4));

	System.out.println(".....");
	System.out.println("Subscribing with the event channel...");
	System.out.println(".....");

	IceGrid.QueryPrx query = IceGrid.QueryPrxHelper.checkedCast(communicator().stringToProxy("IceGrid/Query"));
	IceStorm.TopicManagerPrx manager = IceStorm.TopicManagerPrxHelper.checkedCast(query.findObjectById(communicator().stringToIdentity("Hesperia.Oreto.IceStorm/TopicManager")));

	//
        // Retrieve the topic.
        //
        IceStorm.TopicPrx topic;
        try {
	    topic = manager.retrieve(topic_name);
	}
        catch(IceStorm.NoSuchTopic e) {
            try {
		topic = manager.create(topic_name);
	    }
            catch(IceStorm.TopicExists ex) {
                System.err.println(appName() + ": temporary failure, try again.");
                return 1;
            }
        } // Fin catch NoSuchTopic.
	
	java.util.Map<String, String> qos = new java.util.HashMap<String, String>();
	qos.put("reliability", "ordered");

	try {
	    topic.subscribeAndGetPublisher(qos, subscriber);
	    System.out.println(subscriber.toString() + " is now subscribed!");
	}
        catch(IceStorm.AlreadySubscribed e) {
	    System.out.println(subscriber.toString() + " was already subscribed!");
        }
        catch(IceStorm.BadQoS e) {
            e.printStackTrace();
        }

	adapter.activate();

	communicator().waitForShutdown();

	return 0;
    }

    public static void main(String[] args)
    {
	ReasoningServer rs = new ReasoningServer();
	//int status = rs.main("ReasoningServer", args, "../config/config.server");
	int status = rs.main("ReasoningServer", args);

	System.exit(status);
    }

}
